{% extends "base_generic.html" %}
{% load static %}

{% block content %}
<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-primary text-white">
            <h1 class="mb-0"><i class="fas fa-book"></i> {% if form.instance.pk %}编辑书籍{% else %}添加书籍{% endif %}</h1>
        </div>
        <div class="card-body">
            <form action="" method="post">
                {% csrf_token %}
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="id_title" class="form-label">书名:</label>
                        <input type="text" id="id_title" name="title" class="form-control" value="{{ form.instance.title|default:'' }}" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_author" class="form-label">作者:</label>
                        <select id="id_author" name="author" class="form-select" required>
                            {% for author in form.author.field.queryset %}
                                <option value="{{ author.id }}" {% if form.instance.author.id == author.id %}selected{% endif %}>{{ author }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_summary" class="form-label">摘要:</label>
                        <textarea id="id_summary" name="summary" class="form-control">{{ form.instance.summary|default:'' }}</textarea>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_isbn" class="form-label">ISBN:</label>
                        <input type="text" id="id_isbn" name="isbn" class="form-control" value="{{ form.instance.isbn|default:'' }}">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_genre" class="form-label">类型:</label>
                        <select id="id_genre" name="genre" class="form-select" multiple>
                            {% for genre in form.genre.field.queryset %}
                                <option value="{{ genre.id }}" {% if genre in form.instance.genre.all %}selected{% endif %}>{{ genre }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="id_language" class="form-label">语言:</label>
                        <select id="id_language" name="language" class="form-select">
                            {% for language in form.language.field.queryset %}
                                <option value="{{ language.id }}" {% if form.instance.language.id == language.id %}selected{% endif %}>{{ language }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="text-end">
                    <a href="{% url 'books' %}" class="btn btn-secondary">取消</a>
                    <button type="submit" class="btn btn-primary">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}